package cn.com.shopec.erp.stock.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.Transformers;
import org.hibernate.type.StandardBasicTypes;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import cn.com.shopec.erp.common.constant.SZSCMConstant;
import cn.com.shopec.erp.common.orm.impl.HibernateEntityDao;
import cn.com.shopec.erp.stock.commo.ObjectUtil;
import cn.com.shopec.erp.stock.dao.IStockInProductDao;
import cn.com.shopec.erp.stock.entity.StockInProduct;
import cn.com.shopec.erp.stock.vo.StockInProductVo;

/**
 * 
 * @ClassName: StockInProductImpl
 * @Description: 入库商品Dao接口实现类
 * @author: zengxuezhou
 * @date: 2016年11月28日 下午2:23:05
 */
@Repository
public class StockInProductImpl extends HibernateEntityDao<StockInProduct> implements IStockInProductDao {

	@Override
	public void removeProductByStockInNo(String stockInNo) {

		// String hql = "delete from StockInProduct as p where p.stockInNo = ?";
		String hql = "update StockInProduct as p set p.deletedFlag = " + SZSCMConstant.DELETE_FLAG_YES
				+ " where p.stockInNo = ?";
		getSession().createQuery(hql).setString(0, stockInNo).executeUpdate();
	}

	

	@Override
	public boolean removeForUpdate(String stockInNo) {
		DetachedCriteria criteria = createCriteria();
		criteria.add(Restrictions.eq("deletedFlag", SZSCMConstant.DELETE_FLAG_NO));
		criteria.add(Restrictions.eq("stockInNo", stockInNo));
		List<StockInProduct> stockInProducts = findByCriteria(criteria);// 查询入库单产品信息
		if (!CollectionUtils.isEmpty(stockInProducts)) {
			StringBuilder hql = new StringBuilder("update StockInProduct as p set p.deletedFlag = "
					+ SZSCMConstant.DELETE_FLAG_YES + " where p.id in (");
			for (StockInProduct stockInProduct : stockInProducts) {
				hql.append(" '" + stockInProduct.getId() + "',");
			}
			hql.deleteCharAt(hql.length() - 1);
			hql.append(" )");
			return getSession().createQuery(hql.toString()).executeUpdate() > 0;
		}
		return true;
	}

	@SuppressWarnings({ "unchecked" })
	@Override
	public List<StockInProductVo> getProductsListByStockNo(String stockNo, Integer pageCount, Integer pageSize) {
		String sql = "SELECT S.ID AS id," + " S.STOCKIN_NO AS stockInNo," + " S.SUP_NO AS supNo,"
				+ " S.PROD_NO AS prodNo," + " S.PUR_PROD_NO AS purProdNo," + " S.COMMO_NO AS commoNo,"
				+ " S.PROD_DESC AS prodDesc," + " S.PROD_COST AS prodCost," + " S.PROD_TOTAL AS prodTotal,"
				+ " S.CREATE_TIME AS createTime," + " S.CREATER AS creater,"
				+ " S.QUALIFIED_QUANTITY AS qualifiedQuantity," + " S.DEFECTIVE_QUANTITY AS defectiveQuantity,"
				+ " S.PRODUCT_CODE_STATE AS productCodeState," + " S.DEFECTIVE_REASON AS defectiveReason,"
				+ " C.COMMO_NAME AS commoName," + " S.SUP_NAME AS supName," + " SVO.OPTION_NO AS specification,"
				+ " GROUP_CONCAT(SVO.OPTION_NAME) AS specName," + " PROD.PROD_BAR_CODE AS prodBarCode"
				+ " FROM TBL_STOCKIN_PRODUCT S" + " LEFT JOIN TBL_PRODUCT PROD ON PROD.PROD_NO = S.PROD_NO"
				+ " LEFT JOIN TBL_COMMODITY C ON C.COMMO_NO = S.COMMO_NO"
				+ " LEFT OUTER JOIN TBL_PROD_SPEC_VALUE PSV ON PSV.PROD_NO = S.PROD_NO"
				+ " LEFT OUTER JOIN TBL_SPEC_VAL_OPTION SVO ON SVO.OPTION_NO = PSV.VAL_OPTION_NO"
				+ " WHERE S.STOCKIN_NO = ? GROUP BY S.ID";
		Query query = getSession().createSQLQuery(sql).addScalar("id", StandardBasicTypes.STRING)
				.addScalar("stockInNo", StandardBasicTypes.STRING).addScalar("supNo", StandardBasicTypes.STRING)
				.addScalar("supName", StandardBasicTypes.STRING).addScalar("prodNo", StandardBasicTypes.STRING)
				.addScalar("commoNo", StandardBasicTypes.STRING).addScalar("prodDesc", StandardBasicTypes.STRING)
				.addScalar("purProdNo", StandardBasicTypes.STRING).addScalar("prodCost", StandardBasicTypes.DOUBLE)
				.addScalar("prodTotal", StandardBasicTypes.LONG)
				.addScalar("productCodeState", StandardBasicTypes.INTEGER)
				.addScalar("qualifiedQuantity", StandardBasicTypes.LONG)
				.addScalar("defectiveQuantity", StandardBasicTypes.LONG)
				.addScalar("commoName", StandardBasicTypes.STRING).addScalar("specification", StandardBasicTypes.STRING)
				.addScalar("creater", StandardBasicTypes.STRING).addScalar("defectiveReason", StandardBasicTypes.STRING)
				.addScalar("createTime", StandardBasicTypes.DATE).addScalar("prodBarCode", StandardBasicTypes.STRING)
				.addScalar("specName", StandardBasicTypes.STRING);
		query.setParameter(0, stockNo);
		if (ObjectUtil.isNotNull(pageCount)) {
			query.setFirstResult(pageCount).setMaxResults(pageSize);
		}
		query.setResultTransformer(Transformers.aliasToBean(StockInProductVo.class));
		List<StockInProductVo> stockInProductVos = query.list();
		return stockInProductVos;
	}

	@Override
	public List<StockInProductVo> getProductsListByStockNo(String stockNo) {
		return getProductsListByStockNo(stockNo, null, null);
	}

	@Override
	public int getProductsCountByStockNo(String stockNo) {
		DetachedCriteria detachedCriteria = createCriteria();
		detachedCriteria.add(Restrictions.eq("stockInNo", stockNo));
		return getCountByCriteria(detachedCriteria);
	}

	@Override
	public void setProductCodeIsImport(String id, int state) {
		String hql = "update StockInProduct as p set p.productCodeState = " + state + " where p.id = ?";
		getSession().createQuery(hql).setString(0, id).executeUpdate();
	}
}
